var api = require("../../../api.js"), utils = require("../../../utils.js"), app = getApp(), WxParse = require("../../../wxParse/wxParse.js"), p = 1, is_loading_comment = !1, is_more_comment = !0, share_count = 0;

Page({
  data: {
    userInfo: null,
    id: null,
    from_id: null,
    is_self: false,
    goodsDetail: null,
    showCutShare: false,
    show_attr_picker: true,
    cutRes: null,
    cutList: [],
    form: {
      number: 1
    },
    tab_detail: "active",
    tab_comment: "",
    comment_list: [],
    comment_count: {
      score_all: 0,
      score_3: 0,
      score_2: 0,
      score_1: 0
    },
    autoplay: !1,
    hide: "hide",
    show: !1,
    x: wx.getSystemInfoSync().windowWidth,
    y: wx.getSystemInfoSync().windowHeight - 20,
    miaosha_end_time_over: {
      h: "--",
      m: "--",
      s: "--",
      type: 0
    },
    progressW: 0,
    labelLeft: 0
  },
  onLoad: function (option) {
    app.pageOnLoad(this);

    var userInfo = wx.getStorageSync("user_info");
    this.setData({ userInfo: userInfo });
    console.log(userInfo, '--------------');

    this.setData({
      id: option.gid,
      from_id: option.from_id || userInfo.id
    });
    if (this.data.from_id == userInfo.id) {
      this.setData({is_self:true});
    }

    console.log(option.gid, 'dddddddddiiiiiiii');
    this.loadData();
    this.getCutInfo();
  },
  onReady: function () { },
  onShow: function () { },
  onHide: function () { },
  onUnload: function () { },
  onPullDownRefresh: function () { },
  onReachBottom: function () {
    // this.getCommentList(!0);
  },
  onShareAppMessage: function () {
    var a = this, t = wx.getStorageSync("user_info");
    console.log("/pages/cut/detail/detail?gid=" + a.data.id + "&from_id=" + t.id, '==============');
    return {
      path: "/pages/cut/detail/detail?gid=" + a.data.id + "&from_id=" + t.id,
      success: function (t) {
        console.log(t), 1 == ++share_count && app.shareSendCoupon(a);
        a.setData({ showCutShare: false });
        wx.navigateTo({
          url: "/pages/cut/detail/detail?gid=" + a.data.id
        })
      },
      title: a.data.goodsDetail.name,
      imageUrl: a.data.goodsDetail.pic_list[0].pic_url
    };
  },
  loadData: function () {
    var that = this;
    var rData = {
      id: that.data.id
    };
    this.data.from_id && (rData.from_id = this.data.from_id);
    wx.showLoading({
      title: "加载中..."
    });
    app.request({
      url: api.cut.detail,
      data: rData,
      success: function (res) {
        if (res.code == 0) {
          that.setData({
            goodsDetail: res.data
          });
          if (that.data.goodsDetail.bargain_master){
            var totalW = that.data.goodsDetail.original_price - that.data.goodsDetail.price;
            var whiteW = Number(that.data.goodsDetail.bargain_master.price);
            var wRate = (whiteW / totalW) * 100;
            var labelL = 100 - wRate;
            that.setData({ progressW: wRate, labelL });
          }
 
          WxParse.wxParse("detail", "html", res.data.detail, that);
          that.countTimer();
        }
      },
      fail: function (err) {
        wx.showToast({
          title: '获取砍价商品详情失败',
          image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
        });
      },
      complete: function (d) {
        wx.hideLoading();
      }
    })
  },
  countTimer: function () {
    var t = this;
    if (this.data.goodsDetail && this.data.goodsDetail.second) {
      var timer = setInterval(function () {
        if (0 < t.data.goodsDetail.second) {
          t.data.goodsDetail.second = t.data.goodsDetail.second - 1;
          t.data.goodsDetail.times = t.getTimesBySecond(t.data.goodsDetail.second);
          // console.log(t.data.goodsDetail, '=============');
          t.setData({
            goodsDetail: t.data.goodsDetail
          })
        } else {
          clearInterval(timer);
        }
      }, 1000);
    }
  },
  getTimesBySecond: function (t) {
    if (t = parseInt(t), isNaN(t)) return {
      h: "00",
      m: "00",
      s: "00"
    };
    var a = parseInt(t / 3600), e = parseInt(t % 3600 / 60), i = t % 60;
    return {
      h: a < 10 ? "0" + a : "" + a,
      m: e < 10 ? "0" + e : "" + e,
      s: i < 10 ? "0" + i : "" + i
    };
  },
  getCutInfo: function () {
    var that = this;
    app.request({
      url: api.cut.cutList,
      success: function (res) {
        if (res.code == 0) {
          that.setData({ cutList: res.data.list });
        }
      },
      fail: function (err) {
        wx.showToast({
          title: '获取砍价详情失败',
          image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
        });
      },
      complete: function () {

      }
    });
  },
  fireBargin: function () {
    var that = this;
    wx.showLoading({
      title: "发起砍价中..."
    });
    app.request({
      url: api.cut.doCut,
      method: 'POST',
      data: {
        from_id: that.data.from_id,
        goods_id: that.data.id
      },
      success: function (res) {
        wx.hideLoading();
        
        if (res.code == 0) {
          // console.log(res, '+++++++++++');
          that.setData({
            cutRes: res.data,
            showCutShare: true
          });
          wx.showToast({
            title: '砍价成功',
            image: "http://runwu100.img.guoxiaoge.cn/icon-check.png"
          });
          that.getCutInfo();
          that.loadData();
        } else {
          wx.showToast({
            title: '该商品您已砍价',
            image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
          });
        }
      },
      fail: function (err) {
        wx.hideLoading();
        
        wx.showToast({
          title: '砍价失败',
          image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
        });
      },
      complete: function (res) {
      }
    })
  },
  closeModal: function () {
    this.setData({ showCutShare: false });
  },
  fireBarginToo: function (e) {
    var that = this;
    wx.navigateTo({
      url: "/pages/cut/detail/detail?gid=" + that.data.id
    })
  },
  buyNow: function () {
    var that = this;
    // if (Number(this.data.goodsDetail.bargain_master.price) > Number(this.data.goodsDetail.price)) {
    //   wx.showToast({
    //     title: "砍价未完成",
    //     image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
    //   });
    //   return;
    // }
    wx.redirectTo({
      url: "/pages/cut/order-submit/order-submit?goods_info=" + JSON.stringify({
        goods_id: that.data.goodsDetail.id,
        num: 1,
        deliver_type: 1
      })
    });
  },
});